function LoginController($scope,$state,Notification,FileUploader,LoginService,loadContext,ErrorUtils,context){
	Notification.success({message: 'Please login to begin.', title: 'Welcome to CleanEstimateX'});
	
	$scope.loginText = "Login";
	$scope.showLoading = false;
	
	$scope.login = function(){
		
		$scope.toggleLoading();
		
		var data = "email="+$scope.email+"&password="+$scope.password;
		var login = LoginService.save(data);
		
		login.$promise.then(
				function(data){
					$scope.toggleLoading();
					if(data.meta.code == 200){
						Notification.success({message: 'Logged in successfully.', title: 'Success'});
						
						context.setEmployee(data.data);
						
						//Store employee in session storage
						sessionStorage.setItem('employee',JSON.stringify(context.getEmployee()) );
						
						loadContext.redirectToHomePage();
					}
					else{
						Notification.error({message:ErrorUtils.getMessageByErrorCode(data.meta.code), title: 'Error'});
					}
					
				},
				function(error){
					$scope.toggleLoading();
					Notification.error({message: "Some error occurred. Please try again later.", title: 'Error'});
				});
		
	};
	
	$scope.redirectToHomePage = function(){
		if(context !=null && context.isReady()){
			//Output details
			if(window.console)console.log("Employee:",context.getEmployee());
			
			if(context.getEmployee().company.companyType == "Supplier"){
				$state.go("supplier.machines");
			}
		}
	}
	
	$scope.toggleLoading = function(){
		if($scope.loginText == "Login"){
			$scope.loginText = "Signing in. Please wait..";
			$scope.showLoading = true;
		}
		else{
			$scope.loginText = "Login";
			$scope.showLoading = false;
		}
	}
	
	//Remove the employee from session storage and context.
	sessionStorage.removeItem("employee");	
	context.clearContext();
	
	
}


angular.module('login',['ngAnimate','ui.router','ui-notification','angularFileUpload'])
	.controller('LoginController',['$scope','$state','Notification','FileUploader','LoginService','loadContext','ErrorUtils',LoginController]);